import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Login from '../views/login.vue'
import Register from '../views/register.vue'
import Home from '../views/home.vue'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/register',
      name: 'Register',
      component: Register
    },
    {
      path: '/home',
      name: 'Home',
      component: Home
    }     
  ]
},)


router.beforeEach((to,from,next) => {
  //to将要访问的路径
  //from从哪个路径跳转
  //next()放行   , next('/login')强制跳转
  if(to.path === '/login') return next();
  else if (to.path === '/register') return next();
  //account
  let account = window.sessionStorage.getItem('account');
    if(!account||account=='') {
      
      return next('/login');
    }
  next();

})

export default router
